package com.example.sihookuiklydemo.base

import com.tencent.kuikly.core.base.Animation
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.Translate
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.layout.undefined
import com.tencent.kuikly.core.layout.valueEquals
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.timer.setTimeout
import com.tencent.kuikly.core.views.ActivityIndicator
import com.tencent.kuikly.core.views.Image
import com.tencent.kuikly.core.views.SwitchView
import com.tencent.kuikly.core.views.Text
import com.tencent.kuikly.core.views.View

class ToastDialogView : ComposeView<ToastDialogAttr, ToastDialogEvent>() {

    var animated: Boolean by observable(false)
    override fun created() {
        super.created()
        setTimeout(1000) {
            animated = false


        }
    }

    override fun attr(init: ToastDialogAttr.() -> Unit) {

        super.attr(init)
    }

    override fun body(): ViewBuilder {
        var ctx = this

        return {
            attr {
                absolutePosition(0f, 0f, 0f, 0f)
                when (ctx.attr.toastGravity) {
                    ToastGravity.TOP -> {
                        justifyContentFlexStart()
                    }

                    ToastGravity.CENTER -> {
                        justifyContentCenter()
                    }

                    else -> {
                        justifyContentFlexEnd()
                    }
                }

                alignItemsCenter()

                if (ctx.animated) {
                    backgroundColor(Color(0, 0, 0, 0.5f))
                } else {
                    backgroundColor(Color(0, 0, 0, 0f))
                }
                animation(Animation.springEaseIn(0.5f, 0.92f, 1f), ctx.animated)
            }

            View {
                attr {
                    allCenter()
                    size(ctx.pagerData.pageViewWidth - 24f, 60f)
                    marginLeft(12f)
                    marginRight(12f)
                    backgroundColor((getPager() as BasePager).theme.colors.toastBackground)
                    borderRadius(8f)
                    var percentageYStart = 1f
                    var percentageYEnd = 0f
                    when (ctx.attr.toastGravity) {
                        ToastGravity.BOTTOM -> {
                            percentageYStart = 0f
                            percentageYEnd = 1f
                            marginBottom(100f)
                        }

                        ToastGravity.TOP -> {
                            marginTop(100f)
                        }

                        else -> {
                            percentageYStart = 0f
                            percentageYEnd = 1f
                        }
                    }
                    if (ctx.animated) {
                        transform(Translate(0f, percentageYStart))
                    } else {
                        transform(Translate(0f, percentageYEnd))
                    }


                    animation(Animation.linear(0.1f), ctx.animated)
                }
                event {
                    animationCompletion {
                        if (!ctx.animated) {
                            ctx.event.disMissHandler?.invoke()
                        }
                    }
                }
                Text {
                    attr {
                        ctx.attr.msg?.let {
                            text(it)
                        }
                        color(Color.WHITE)
                        useDpFontSizeDim(true)
                        textAlignCenter()
                    }
                }
            }


        }
    }

    //
    override fun createAttr(): ToastDialogAttr = ToastDialogAttr()

    override fun createEvent(): ToastDialogEvent = ToastDialogEvent()
    override fun viewDidLayout() {
        super.viewDidLayout()
        animated = true
    }

}

enum class ToastGravity {
    TOP, CENTER, BOTTOM
}

class ToastDialogAttr : ComposeAttr() {
    var msg: String? = null
    var toastGravity: ToastGravity = ToastGravity.CENTER
}

class ToastDialogEvent : ComposeEvent() {

    var disMissHandler: (() -> Unit)? = null


    fun onDissMiss(handler: () -> Unit) {
        disMissHandler = handler
    }
}

/*
*  UISwitch开关，风格类iOS UISwitch开关，尺寸默认(51f, 31f), 支持手动设置指定size(xx, xx)
*/
fun ViewContainer<*, *>.ToastDialog(init: ToastDialogView.() -> Unit) {
    addChild(ToastDialogView(), init)
}